package com.mkyong;

import java.util.Date;

import com.dmgo.common.Station;
import com.dmgo.common.Train;
import org.hibernate.Session;

import com.mkyong.stock.Stock;
import com.mkyong.stock.StockDailyRecord;
import com.mkyong.util.HibernateUtil;

public class App {
    public static void main(String[] args) {
        System.out.println("Hibernate one to many (Annotation)");
        Session session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
/*
        Stock stock = new Stock();
        stock.setStockCode("7052");
        stock.setStockName("PADINI");
        session.save(stock);
        
        StockDailyRecord stockDailyRecords = new StockDailyRecord();
        stockDailyRecords.setPriceOpen(new Float("1.2"));
        stockDailyRecords.setPriceClose(new Float("1.1"));
        stockDailyRecords.setPriceChange(new Float("10.0"));
        stockDailyRecords.setVolume(3000000L);
        stockDailyRecords.setDate(new Date());
        
        stockDailyRecords.setStock(stock);        
        stock.getStockDailyRecords().add(stockDailyRecords);

        session.save(stockDailyRecords);

        System.out.println(stock.toString());
        System.out.println(stockDailyRecords.toString());

        session.delete(stockDailyRecords);
        session.delete(stock);
*/


        Station station = new Station("station 1", "descr");

        session.save(station);
        System.out.println(station.toString());

        Train train1 = new Train();
        train1.setStation(station);
        session.save(train1);
        System.out.println(train1.toString());



        Train train2 = new Train();
        train2.setStation(station);

        session.save(train2);



        for (Train train : station.getTrains()) {
            System.out.println("train " + train.toString());
        }




        session.delete(train2);




        session.delete(train1);
        session.delete(station);

        session.getTransaction().commit();
        System.out.println("Done");
    }
}
